package com.llkj.contractmanagement.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/***
 * * @description
 * * 由 马君 创建于 2015年11月04日 09:18
 ***/
public class CacheFileUtils {

    private static CacheFileUtils cacheFileUtils;
    private Context context;

    public static final String FOLDER_MAIN=getBasePath()+"/ContractManagement";
    public static final String FOLDER_CACHE=FOLDER_MAIN+"/download";//下载文件夹

    private CacheFileUtils(){
        initCacheFolders();
    }

    public static CacheFileUtils getInstance(Context context){
        if(cacheFileUtils==null){
            synchronized (CacheFileUtils.class){
                if(cacheFileUtils==null){
                    cacheFileUtils=new CacheFileUtils();
                }
            }
        }
        cacheFileUtils.context=context;
        return cacheFileUtils;
    }

    public void setFolder(){

    }

    public void initCacheFolders(){
        createFolderIfNotExist(FOLDER_MAIN);
        createFolderIfNotExist(FOLDER_CACHE);
    }

    public void createFolderIfNotExist(String fileName){
        File file=new File(fileName);
        if(!file.exists()){
            file.mkdirs();
            LogUtil.e("File->文件夹->" + fileName + "被创建");
        }
    }

    public File openOrCreateFile(String fileName){
        File file=new File(fileName);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    public boolean isFileExists(String filePath){
        if(new File(filePath).exists()){
            return true;
        }else{
            return false;
        }
    }

    public static String getBasePath(){
        List<String> sdpath=getExtSDCardPath();
        if(sdpath.size()>0){
            return sdpath.get(0);
        }
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    /**
     * 获取外置SD卡路径
     * @return  应该就一条记录或空
     */
    public static List<String> getExtSDCardPath()
    {
        List<String> lResult = new ArrayList<>();
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("mount");
            InputStream is = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                if (line.contains("extSdCard"))
                {
                    String[] arr = line.split(" ");
                    String path = arr[1];
                    File file = new File(path);
                    if (file.isDirectory())
                    {
                        lResult.add(path);
                    }
                }
            }
            isr.close();
        } catch (Exception e) {
        }
        return lResult;
    }

}
